<?php
namespace Common\Model;

use Think\Model;

class LogGoldModel extends Model
{
    protected $_validate = array(//array('title','require','问题标题必须填写！',1),
    );

    protected $_auto = array(//array('issolve','0'),
    );

    /**
     * 获取列表
     *
     * @param string $map
     * @param bool $page
     * @param int $limit
     * @return mixed
     */
    public function log_list($map = '', $page = true, $limit = 10)
    {
        if ($page) {
            $count = $this->where($map)->count();
            $Page = pager($count, limit);
            $list = $this->where($map)->order('id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else {
            $list = $this->where($map)->order('id DESC')->limit($limit)->select();
        }
        $return['list'] = $list;
        $return['count'] = $page ? $count : count($list);
        $return['page'] = $page ? $Page->fshow() : '';
        return $return;
    }

    /**
     * 添加用户积分/经验变更日志
     *
     * @param string $type '1'-积分 '2'-经验
     * @param int $uid
     * @param int $var 积分/经验变动值
     * @param string $logtxt 日志描述
     * @param bool $addition 变动方向 true-添加 false-减少
     * @return bool
     */
    public function add_log($type = '1', $uid = null, $var = 0, $logtxt = '', $addition = true)
    {
        $var = intval($var);
        $data['type'] = $type;
        $data['uid'] = intval($uid);
        if ($type == '1') {
            $data['gold'] = $addition ? $var : ("-" . $var);
            $data['exp'] = 0;
        }
        if ($type == '2') {
            $data['gold'] = 0;
            $data['exp'] = $addition ? $var : ("-" . $var);
        }
        $data['addtime'] = time();
        $data['txt'] = $logtxt;
        $data['entrance'] = C('YTATA_ENTRANCE');
        $this->add($data);
        return true;
    }

}